﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyPublisher.Windows.Controls
{
    public class WindowBase : System.Windows.Window
    {
        public WindowBase()
        {
            if (Application.Current != null && Application.Current.MainWindow != null && Application.Current.MainWindow != this)
            {
                this.Owner = Application.Current.MainWindow;
                this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            }
            else
                this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.Loaded += new RoutedEventHandler(CAWindow_Loaded);
        }

        protected void CAWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.ActualHeight > SystemParameters.WorkArea.Height)
            {
                this.Height = SystemParameters.WorkArea.Height;
            }
            if (this.ActualWidth > SystemParameters.WorkArea.Width)
            {
                this.Width = SystemParameters.WorkArea.Width;
            }
            if (this.Top < 0)
            {
                this.Top = 0;
            }
            if (this.Top + this.ActualHeight > SystemParameters.WorkArea.Bottom)
            {
                this.Top -= (this.Top + this.ActualHeight - SystemParameters.WorkArea.Bottom);
            }
            if (this.Left < 0)
            {
                this.Left = 0;
            }
            if (this.Left + this.ActualWidth > SystemParameters.WorkArea.Right)
            {
                this.Left -= (this.Left + this.ActualWidth - SystemParameters.WorkArea.Right);
            }
        }
    }
}
